// pages/login/login.js
const app = getApp();
import axios from "../../http/axios.js";
// import {
//   regExpjobNumber
// } from "../../utils/regular.js";
import {
  apiWorkerLogin
} from "../../http/server.js";
import {
  md5
} from "../../utils/md5.js";
Page({
  /**
   * 页面的初始数据
   */
  data: {
    jobNumber: "",
    password: ""
  },
  onLoad: function(options) {

  },
  onReady: function() {

  },
  onShow: function() {

  },
  // 密码输入
  passwordInput: function(e) {
    this.setData({
      password: e.detail.value
    })
  },
  // 工号输入
  jobNumberInput: function(e) {
    this.setData({
      jobNumber: e.detail.value
    })
  },
  // 校验
  check: function() {
    let jobNumber = this.data.jobNumber;
    let password = this.data.password;
    if (!jobNumber.length) {
      wx.showToast({
        title: "请输入工号",
        icon: "none"
      });
      return false;
    }
    // if (!regExpjobNumber.test(jobNumber)) {
    //   wx.showToast({
    //     title: "请输入正确的工号",
    //     icon: "none"
    //   })
    //   return false;
    // }
    if (!password.length) {
      wx.showToast({
        title: "请输入密码",
        icon: "none"
      });
      return false;
    }
    return true;
  },
  // 职工点击登录
  login: function(e) {
    // 先校验
    if (!this.check()) {
      return;
    }
    let data = {
      jobNumber: this.data.jobNumber,
      password: md5(this.data.password),
      openid: wx.getStorageSync("userInfo").openid
    }
    axios({
      url: apiWorkerLogin,
      method: "POST",
      data: data
    }).then(res => {
      if (res) {
        wx.setStorageSync("userInfo", res);
        wx.reLaunch({
          url: "/pages/n_index/n_index",
        })
      }
    })
  }
})